package com.djbx.points.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djbx.points.common.core.utils.BeanListUtils;
import com.djbx.points.common.core.utils.PageList;
import com.djbx.points.common.core.utils.ServiceResponse;
import com.djbx.points.coupon.api.dto.CouponInDTO;
import com.djbx.points.coupon.api.dto.CouponOutDTO;
import com.djbx.points.coupon.entity.CouponEntity;
import com.djbx.points.coupon.mapper.CouponMapper;
import com.djbx.points.coupon.service.CouponService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Override
    @GlobalTransactional
    public ServiceResponse<Void> save(CouponInDTO inDTO) {
        CouponEntity entity = new CouponEntity();
        BeanUtils.copyProperties(inDTO, entity);
        int num = couponMapper.insert(entity);
        log.info("coupon信息入库{}条", num);
        return new ServiceResponse<>();
    }

    @Override
    @GlobalTransactional
    public ServiceResponse<Void> update(CouponInDTO inDTO) {
        CouponEntity entity = new CouponEntity();
        BeanUtils.copyProperties(inDTO, entity);
        int num = couponMapper.updateById(entity);
        log.info("coupon信息更新{}条", num);
        return new ServiceResponse<>();
    }

    @Override
    @GlobalTransactional
    @SuppressWarnings("check")
    public ServiceResponse<Void> deleteBatch(List<Long> ids) {
        couponMapper.deleteBatchIds(ids);
        return new ServiceResponse<> ();
    }

    @Override
    public ServiceResponse<CouponOutDTO> getDetail(Long id) {
        CouponEntity entity = couponMapper.selectById(id);
        CouponOutDTO outDTO = new CouponOutDTO();
        BeanUtils.copyProperties(entity, outDTO);
        log.info("获取到CouponOutDTO信息{}", outDTO);
        return new ServiceResponse<CouponOutDTO>().setData(outDTO);
    }

    @Override
    public ServiceResponse<CouponOutDTO> getList(CouponInDTO inDTO) {
        LambdaQueryWrapper<CouponEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isBlank(inDTO.getCouponName()), CouponEntity::getCouponName, inDTO.getCouponName());
        wrapper.like(!Objects.isNull(inDTO.getMemberLevel()), CouponEntity::getMemberLevel, inDTO.getMemberLevel());
        wrapper.orderByAsc(CouponEntity::getStartTime);

        List<CouponEntity> entities = couponMapper.selectList(wrapper);
        List<CouponOutDTO> dtoList = BeanListUtils.copyListProperties(entities, CouponOutDTO::new);
        return new ServiceResponse<CouponOutDTO>().setList(dtoList);
    }

    @Override
    public ServiceResponse<CouponOutDTO> getPage(CouponInDTO inDTO) {
        IPage<CouponEntity> page = new Page<>();
        page.setCurrent(inDTO.getCurrPage());
        page.setSize(inDTO.getPageSize());

        LambdaQueryWrapper<CouponEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isBlank(inDTO.getCouponName()), CouponEntity::getCouponName, inDTO.getCouponName());
        wrapper.like(!Objects.isNull(inDTO.getMemberLevel()), CouponEntity::getMemberLevel, inDTO.getMemberLevel());
        wrapper.orderByAsc(CouponEntity::getStartTime);

        IPage<CouponEntity> entityPage = couponMapper.selectPage(page, wrapper);
        PageList<CouponOutDTO> dtoPage = BeanListUtils.copyPageProperties(entityPage, CouponOutDTO::new);
        return new ServiceResponse<CouponOutDTO>().setPageList(dtoPage);
    }
}